/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * preferences.c
 *
 * Copyright (C) 2008, 2009 Lingtao Pan
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA 02111-1307, USA.
 */


#include "common.h"



#define PREFERENCE_UI PKGDATADIR "/preference-dialog.ui"


static GtkBuilder *builder;
static GtkDialog  *pdialog;
static GtkEntry *remote_charset_entry;
static GtkEntry *local_startup_entry;
static GtkSpinButton *max_dlrate_spbtn;
static GtkCheckButton *show_hf_checkbtn;

static GtkSpinButton *num_retries_spbtn;
static GtkSpinButton *retry_interval_spbtn;
static GtkSpinButton *network_io_timeout_spbtn;


void
on_pdialog_close (GtkDialog *dialog, gint response_id, 
                       gpointer user_data)
{
   
}


static void
preferences_response_cb (GtkDialog *dlg, gint res_id, gpointer data)
{
	switch (res_id) {
        
    default:
        dprintf ("reponse id is %d, call gftp_config_save\n", res_id);
        gftp_config_save ();
        gtk_widget_hide (GTK_WIDGET (dlg));
	}
}

static void check_refresh (GObject *obj)
{
    gboolean refresh;

    refresh = (gboolean) g_object_get_data (obj, "NeedRefresh");

    if (refresh) {
        refresh_left ();
        refresh_right ();
    }
}

static void
preferences_entry_edited_cb (GtkEntry *entry, gpointer data)
{
	gchar *group;
    gchar *key;
    const gchar *value;

    group = (gchar *) g_object_get_data (G_OBJECT (entry), "Group");
    key = (gchar *) g_object_get_data (G_OBJECT (entry), "Key");
    
    value = gtk_entry_get_text (entry);

    gftp_config_set_string (group, key, value); 

    check_refresh (G_OBJECT (entry));
}

static void
preferences_checkbtn_edited_cb  (GtkToggleButton *togglebutton, gpointer user_data)
{
    gchar *group;
    gchar *key;
    gboolean value;

    group = (gchar *) g_object_get_data (G_OBJECT (togglebutton), "Group");
    key = (gchar *) g_object_get_data (G_OBJECT (togglebutton), "Key");
    value = gtk_toggle_button_get_active (togglebutton);

    gftp_config_set_boolean (group, key, value);

    check_refresh (G_OBJECT (togglebutton));
}

static void
preferences_spinbtn_cb (GtkSpinButton *spinbutton, gpointer user_data)
{
    gchar *group;
    gchar *key;
    gdouble value;

    group = (gchar *) g_object_get_data (G_OBJECT (spinbutton), "Group");
    key = (gchar *) g_object_get_data (G_OBJECT (spinbutton), "Key");
    value = gtk_spin_button_get_value (spinbutton);

    gftp_config_set_double (group, key, value);

    check_refresh (G_OBJECT (spinbutton));
} 


static void 
preferences_connect_signals ()
{
    g_assert (builder != NULL);

    g_object_set_data (G_OBJECT (remote_charset_entry), "Group", "General");
    g_object_set_data (G_OBJECT (remote_charset_entry), "Key", 
                                            "remote_charsets");
    g_object_set_data (G_OBJECT (remote_charset_entry), "NeedRefresh", 
                                           (gpointer) TRUE);
    g_signal_connect (remote_charset_entry, "activate",
                      G_CALLBACK (preferences_entry_edited_cb), NULL);

    g_object_set_data (G_OBJECT (local_startup_entry), "Group", "General");
    g_object_set_data (G_OBJECT (local_startup_entry), "Key", 
                                             "local_startup_directory");
    g_object_set_data (G_OBJECT (local_startup_entry), "NeedRefresh", 
                                          (gpointer) FALSE);
    g_signal_connect (local_startup_entry, "activate", 
                      G_CALLBACK (preferences_entry_edited_cb), NULL);


    g_object_set_data (G_OBJECT (show_hf_checkbtn), "Group", "General");
    g_object_set_data (G_OBJECT (show_hf_checkbtn), "Key", 
                                      "show_hidden_files");
    g_object_set_data (G_OBJECT (show_hf_checkbtn), "NeedRefresh", 
                                           (gpointer) TRUE);
    g_signal_connect (show_hf_checkbtn, "toggled", 
                      G_CALLBACK (preferences_checkbtn_edited_cb), NULL);

    g_object_set_data (G_OBJECT (max_dlrate_spbtn), "Group", "General");
    g_object_set_data (G_OBJECT (max_dlrate_spbtn), "Key", "maxkbs");
    g_object_set_data (G_OBJECT (max_dlrate_spbtn), "NeedRefresh", 
                                           (gpointer) FALSE);
    g_signal_connect (max_dlrate_spbtn, "value-changed", 
                      G_CALLBACK (preferences_spinbtn_cb), NULL);

    g_object_set_data (G_OBJECT (network_io_timeout_spbtn), "Group", "Network");
    g_object_set_data (G_OBJECT (network_io_timeout_spbtn), "Key", "netowkr_timeout");
    g_object_set_data (G_OBJECT (network_io_timeout_spbtn), "NeedRefresh", 
                                           (gpointer) FALSE);
    g_signal_connect (network_io_timeout_spbtn, "value-changed", 
                      G_CALLBACK (preferences_spinbtn_cb), NULL);

    g_object_set_data (G_OBJECT (num_retries_spbtn), "Group", "Network");
    g_object_set_data (G_OBJECT (num_retries_spbtn), "Key", "retries");
    g_object_set_data (G_OBJECT (num_retries_spbtn), "NeedRefresh", 
                                           (gpointer) FALSE);
    g_signal_connect (num_retries_spbtn, "value-changed", 
                      G_CALLBACK (preferences_spinbtn_cb), NULL);

    g_object_set_data (G_OBJECT (retry_interval_spbtn), "Group", "Network");
    g_object_set_data (G_OBJECT (retry_interval_spbtn), "Key", "sleep_time");
    g_object_set_data (G_OBJECT (retry_interval_spbtn), "NeedRefresh", 
                                           (gpointer) FALSE);
    g_signal_connect (retry_interval_spbtn, "value-changed", 
                      G_CALLBACK (preferences_spinbtn_cb), NULL);


    g_signal_connect (G_OBJECT (pdialog), "response",
                      G_CALLBACK (preferences_response_cb), NULL); 
}

static void preferences_set_data ()
{
    gchar *str;
    gboolean b;
    gdouble dval;
    
    str = gftp_config_get_string ("General", "remote_charsets");
    if (str != NULL) {
        gtk_entry_set_text (remote_charset_entry, str);
        g_free (str);
    } else {
        gtk_entry_set_text (remote_charset_entry, "");
    }

    b = gftp_config_get_boolean ("General", "show_hidden_files");
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (show_hf_checkbtn), b);

    dval = gftp_config_get_double ("General", "maxkbs");
    gtk_spin_button_set_value (max_dlrate_spbtn, dval);
    
    str = gftp_config_get_string ("General", "local_startup_directory");
    if (str != NULL) {
        gtk_entry_set_text (local_startup_entry, str);
        g_free (str);
    } else {
        gtk_entry_set_text (local_startup_entry, "");
    }

    dval = gftp_config_get_double ("Network", "network_timeout");
    gtk_spin_button_set_value (network_io_timeout_spbtn, dval);

    dval = gftp_config_get_double ("Network", "retries");
    gtk_spin_button_set_value (num_retries_spbtn, dval);

    dval = gftp_config_get_double ("Network", "sleep_time");
    gtk_spin_button_set_value (retry_interval_spbtn, dval);
}


void
show_preference_dialog_cb (GtkMenuItem *item, gpointer data)
{
    GError *err = NULL;
    
    if (builder == NULL) {
        builder = gtk_builder_new ();
        gtk_builder_add_from_file (builder, PREFERENCE_UI,
                                   &err);
        if (err != NULL) {
            g_warning ("Error in cb_show_preferenc_dialog: %s",
                       err->message);
            return;
        }

   
        pdialog = (GtkDialog *) gtk_builder_get_object (builder, 
                                                        "preference_dialog");
  
        if (pdialog == NULL) {
            g_warning ("Can't get preference dialog from GtkBuilder");
            return;
        }

        g_signal_connect (pdialog, "delete-event", 
                          G_CALLBACK (gtk_widget_hide_on_delete),  NULL);

        remote_charset_entry = (GtkEntry *) gtk_builder_get_object (builder,
                                                  "remote_charset_entry");
        local_startup_entry  = (GtkEntry *) gtk_builder_get_object (builder,
                                                  "local_startup_entry");
        max_dlrate_spbtn = (GtkSpinButton *) 
            gtk_builder_get_object (builder, "max_dlrate_spbtn");

        show_hf_checkbtn = (GtkCheckButton *) 
            gtk_builder_get_object (builder, "show_hf_checkbtn");

        network_io_timeout_spbtn = (GtkCheckButton *) 
            gtk_builder_get_object (builder, "network_io_timeout_spbtn");

        num_retries_spbtn = (GtkCheckButton *) 
            gtk_builder_get_object (builder, "num_retries_spbtn");

        retry_interval_spbtn = (GtkCheckButton *) 
            gtk_builder_get_object (builder, "retry_interval_spbtn");
        
        
        g_assert (remote_charset_entry != NULL);
        g_assert (show_hf_checkbtn != NULL);
        g_assert (local_startup_entry != NULL);
        g_assert (max_dlrate_spbtn != NULL);
        g_assert (network_io_timeout_spbtn != NULL);
        g_assert (num_retries_spbtn != NULL);
        g_assert (retry_interval_spbtn != NULL);

        preferences_set_data ();
        preferences_connect_signals ();
    }

    
    gtk_widget_show_all (GTK_WIDGET (pdialog));
}
